import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Home1_2Page extends StatelessWidget {
  const Home1_2Page({Key? key}) : super(key: key);

  Future<void> _logout(BuildContext context) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool('isLoggedIn', false);
    Navigator.pushNamedAndRemoveUntil(context, '/home0', (route) => false);
  }

  @override
  Widget build(BuildContext context) {
    // 获取屏幕宽度以计算尺寸
    final screenWidth = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color(0xFF1DC4A0),
        title: const Text(
          '医生端',
          style: TextStyle(color: Colors.white),
        ),
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back,
            color: Colors.white, // 设置箭头颜色为白色
          ),
          onPressed: () => Navigator.pop(context), // 设置返回功能
        ),
        actions: [
          IconButton(
            icon: Image.asset('assets/icons/logout.png', width: 96, height: 96),
            onPressed: () => _logout(context),
          ),
        ],
      ),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 顶部区域
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(20),
              decoration: const BoxDecoration(
                color: Color(0xFF1DC4A0),
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(20),
                  bottomRight: Radius.circular(20),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // IconButton(
                  //   icon: const Icon(Icons.arrow_back, color: Colors.white),
                  //   onPressed: () => Navigator.pop(context),
                  //   padding: EdgeInsets.zero,
                  //   alignment: Alignment.centerLeft,
                  // ),
                  const SizedBox(height: 10),
                  Row(
                    children: [
                      CircleAvatar(
                        radius: 30,
                        backgroundColor: Colors.white,
                        // child: Icon(
                        //   Icons.person,
                        //   size: 40,
                        //   color: Color(0xFF1DC4A0),
                        // ),
                        child: ClipOval(
                          child: Image.asset(
                            'assets/icons/doctor_home0.png',
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      SizedBox(width: 15),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '您好，医生',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          // SizedBox(height: 5),
                          // Text(
                          //   '欢迎使用AI辅助诊疗系统',
                          //   style: TextStyle(
                          //     color: Colors.white70,
                          //     fontSize: 16,
                          //   ),
                          // ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),

            // 服务选项卡区域
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      '医生服务',
                      style: TextStyle(
                        fontSize: 22,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 10),
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 7),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            const SizedBox(height: 10),
                            _buildServiceCard(
                              title: 'AI辅助诊断',
                              iconPath: 'assets/icons/icon1_home1-3.png',
                              color: const Color(0xFF8F9EFF),
                              onTap: () {
                                Navigator.pushNamed(context, '/ai_diagnosis');
                              },
                            ),
                            const SizedBox(height: 30),
                            _buildServiceCard(
                              title: 'AI辅助治疗',
                              iconPath: 'assets/icons/icon2_home1-3.png',
                              color: const Color(0xFFFEAA48),
                              onTap: () {
                                Navigator.pushNamed(context, '/ai_treatment');
                              },
                            ),

                            // 以下注释掉的部分保持原样
                            // _buildServiceCard(
                            //   context,
                            //   title: '患者管理',
                            //   icon: Icons.people,
                            //   color: Colors.orange,
                            //   onTap: () {
                            //     // 功能未实现
                            //     ScaffoldMessenger.of(context).showSnackBar(
                            //       const SnackBar(content: Text('此功能正在开发中')),
                            //     );
                            //   },
                            // ),

                            // _buildServiceCard(
                            //   context,
                            //   title: '医疗记录',
                            //   icon: Icons.description,
                            //   color: Colors.purple,
                            //   onTap: () {
                            //     // 功能未实现
                            //     ScaffoldMessenger.of(context).showSnackBar(
                            //       const SnackBar(content: Text('此功能正在开发中')),
                            //     );
                            //   },
                            // ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildServiceCard({
    required String title,
    required String iconPath,
    required Color color,
    required VoidCallback onTap,
  }) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(15),
        child: Container(
          width: double.infinity,
          height: 110,
          padding: const EdgeInsets.symmetric(horizontal: 25),
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(15),
            boxShadow: [
              BoxShadow(
                color: color.withOpacity(0.3),
                blurRadius: 8,
                offset: const Offset(0, 4),
              ),
            ],
          ),
          child: Row(
            children: [
              // 图标
              Container(
                width: 60,
                height: 60,
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Image.asset(
                  iconPath,
                  width: 36,
                  height: 36,
                ),
              ),
              const SizedBox(width: 20),
              // 文本
              Text(
                title,
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
